<%--
  Created by IntelliJ IDEA.
  User: bin.liu
  Date: 2018/12/13
  Time: 10:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<style>
    .deleteImg {
        width: 20px;
        position: relative;
        top: 0px;
        left: 20px;
        cursor: pointer;
        display: none;
    }

    .startImg {
        width: 131px;
        height: 32px;
    }
</style>
<div class="main-content">
    <div class="box box-main">
        <form id="facilityDataForm" model="post" action="<c:url value="/web/admin/facilityData/executeFacilityDataEdit.action"/>"
              method="post" enctype="multipart/form-data" class="form-horizontal">
            <div class="box-body">
                <div class="form-unit">基本信息</div>
                <input type="hidden" name="id" value="${facilityData.id}"/>
                <input type="hidden" name="createDate1"
                       value="<fmt:formatDate value="${facilityData.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"/>
                <input type="hidden" name="serialNum" value="${facilityData.serialNum}"/>
                <div class="row">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span>资料名称：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input path="facilityDataName" name="facilityDataName" value="${facilityData.facilityDataName}" maxlength="100"
                                       class="form-control required">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span>资料描述：</label>
                            <div class="col-sm-8 controls">
                                <textarea htmlEscape="false" rows="4" maxlength="400" name="description"
                                          class="form-control required">${facilityData.description}</textarea>
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span>关联设备：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="selectFacilityId" name="selectFacilityId" class="form-control required">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                <span class="required">*</span>所属分类：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <input id="selectDataCatId" name="selectDataCatId" class="form-control required">
                            </div>
                        </div>
                    </div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                所属企业：<i class="fa icon-question hide"></i></label>
                            <label class="control-label col-sm-8" style="text-align: left" title="">
                                ${enterprise.enterpriseName}<i class="fa icon-question hide"></i></label>
                        </div>
                    </div>
                    <div class="form-unit">上传资料</div>
                    <div class="col-xs-12">
                        <div class="form-group">
                            <label class="control-label col-sm-4" title="">
                                上传资料：<i class="fa icon-question hide"></i></label>
                            <div class="col-sm-8">
                                <div style="width: 300px;">
                                    <img class="startImg" id="img1" src="<c:url value="/resources/images/pdfUpLoad.png" />"
                                         onclick="$('#pic1').click();"/>
                                    <input type="file" style="display: none;" name="pic1" id="pic1" onchange="getimgload(1)"/>
                                    <label class="control-label col-sm-12" title="">
                                        <%--<a href="${filePath}" id="filePath" class="showFileName" target="_blank"></a>--%>
                                            <span class="showFileName"></span>
                                            <img class="deleteImg" id="delete1" src="<c:url value="/resources/images/delete.png"/>" onclick="delectImg(1)"/>
                                    </label>
                                    <input type="hidden" id="sImg1" name="sImg1" value="0"/><%--判断有没有变更图片用的 变了就为1 不变就为0--%>
                                </div>
                            </div>
                            <label class="control-label col-sm-4" title="">
                                （只支持PDF格式）<i class="fa icon-question hide"></i></label>

                        </div>
                    </div>

                </div>

                <div class="box-footer">
                    <div class="row">
                        <div class="col-sm-offset-2 col-sm-12">
                            <button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i
                                    class="fa fa-check"></i> 保 存
                            </button>&nbsp;

                            <button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="closeView()"><i
                                    class="fa fa-times"></i> 关 闭
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    function delectImg(id) {
        $("#sImg" + id).val('1');
        $("#pic" + id).val('');
        $(".showFileName").html("");
        $("#img" + id).attr("src", "<c:url value="/resources/images/pdfUpLoad.png" />");
        $("#delete" + id).hide();
    }
    function getimgload(id) {
        var filePath=$('#pic1').val();
        var arr=filePath.split('\\');
        var fileName=arr[arr.length-1];

        var obj = document.getElementById("pic" + id);
        var stuff = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3];
        if (stuff != 'pdf') {
            alert('只支持pdf文件');
            return false;
        }

        // $("#filePath").attr('href','');
        $(".showFileName").html(fileName);
        $("#sImg" + id).val('1');//表示图片变更了（总之不是原来的图片了）
        $("#delete" + id).show();
    }
    $(function () {
        if (${fileName != null && fileName != ''}) {
            $(".showFileName").html('${fileName}');
            $("#delete1").show();
        }
    });

    $("#facilityDataForm").validate({
        submitHandler: function(form){
            if (!validateMagicSuggest({
                selectDataCatId: '不能为空',
                selectFacilityId: '不能为空',
            })) return false;
            js.ajaxSubmitForm($(form), function(data){
                js.showMessage(data.msg);
                if(data.status == Global.NO){
                    closeView();
                    $('#dataGrid').dataGrid("refresh");
                }
            }, "json");
        }
    });

    function closeView() {
        layer.close(LayWin['wFacilityData']);
    }

    var selectDataCatId = $('#selectDataCatId').magicSuggest({
        allowFreeEntries: false,
        data: '<c:url value="/web/admin/dataCat/getDataCatSelect.action" />',
        queryParam: 'q',
        valueField: "value",
        displayField: "text",
        // maxSelection: 1,
        noSuggestionText: '',
        placeholder: '请选择',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: ${dataCatIds}
    });
    $(selectDataCatId).on('selectionchange', function(e,m){
        validateMagicReload();
    });
    var selectFacilityId = $('#selectFacilityId').magicSuggest({
        allowFreeEntries: false,
        data: '<c:url value="/web/admin/facility/getFacilitySelect.action" />',
        queryParam: 'q',
        valueField: "value",
        displayField: "text",
        // maxSelection: 1,
        noSuggestionText: '',
        placeholder: '请选择',
        required: true,
        autoSelect: true,
        selectFirst: true,
        value: ${facilityIds}
    });
    $(selectFacilityId).on('selectionchange', function(e,m){
        validateMagicReload();
    });
</script>